<template>
  <div class="login-wrapper">
    <h1>拼车系统管理后台</h1>
    <div class="login-contains-wrapper">
      <div class="login-img">
        <img src="../../assets/images/login-bg.png" style="width: 100%; height: 100%; object-fit: cover" />
      </div>
      <div class="login-form">
        <h4>账户登录</h4>
        <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" class="login-form-wrapper">
          <el-form-item prop="username">
            <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="请输入账号">
              <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input :key="passwordType" ref="passwordRef" v-model="loginForm.password" :type="passwordType"
              auto-complete="off" placeholder="请输入登录密码" @keyup.enter.native="handleLogin">
              <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
            </el-input>
            <span class="show-pwd" @click="showPwd">
              <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
            </span>
          </el-form-item>
          <el-form-item prop="captcha" v-if="captchaEnabled">
            <el-input v-model="loginForm.captcha" auto-complete="off" placeholder="请输入验证码" style="width: 63%"
              @keyup.enter.native="handleLogin">
              <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
            </el-input>
            <div class="login-code">
              <img :src="captchaUrl" @click="getCode" class="login-code-img" />
            </div>
          </el-form-item>

          <el-form-item style="width: 100%">
            <el-button :loading="loading" size="medium" type="primary" style="width: 100%; margin-top: 30px"
              @click.native.prevent="handleLogin">
              <span v-if="!loading">立 即 登 录</span>
              <span v-else>登 录 中...</span>
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import config from "@/settings";
const { imgBaseUrl } = config;
import { setToken } from "@/util/auth";
import website from "@/config/website";
export default {
  name: "Login",
  data () {
    const validatePassword = (rule, value, callback) => {
      const regex = /^(?=.*[0-9])(?=.*[a-zA-Z])[a-zA-Z0-9]{8,32}$/;
      if (value.length === 0) {
        callback(new Error("请输入登录密码"));
      } else if (!regex.test(value)) {
        callback(
          new Error("请输入字符长度8-32位，必须同时包含数字、字母的密码")
        );
      } else {
        callback();
      }
    };
    return {
      captchaUrl: "",
      // 验证码开关
      captchaEnabled: true,
      redirect: undefined,
      loginForm: {
        username: "", // admin
        password: "", // 123456
        captcha: "",
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入登录账号" },
        ],
        // password: [
        //   { required: true, trigger: "blur", validator: validatePassword },
        // ],
      },
      isPsd: false,
      loading: false,
      passwordType: "password",
      redirect: undefined,
    };
  },
  computed: {},
  created () {
    this.getCode();
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
    "loginForm.password": {
      handler (n, o) {
        this.$refs.loginFormRef.validate((valid) => {
          if (valid) {
            this.isPsd = true;
          }
        });
      },
    },
  },
  methods: {
    getCode () {
      let that = this;
      that.$http
        .get("/api/admin/common/captcha", {
          responseType: "blob", // 期待二进制数据
        })
        .then((res) => {
          console.log(1111, "图形验证码", res);
          this.captchaUrl = URL.createObjectURL(res);
        });
    },
    // 查看密码
    showPwd () {
      this.passwordType === "password"
        ? (this.passwordType = "")
        : (this.passwordType = "password");
      this.$nextTick(() => {
        this.$refs.passwordRef.focus();
      });
    },
    // 登录
    handleLogin () {
      this.$refs.loginFormRef.validate((valid) => {
        if (valid) {
          this.isPsd = true;
          this.loading = true;
          this.$http
            .post("/api/admin/auth/login", {
              username: this.loginForm.username,
              password: this.loginForm.password,
              captcha: this.loginForm.captcha,
            })
            .then((res) => {
              console.log("登录:1111111111111111111111", res.data);

              // 根据permissions判断用户类型
              const isAgent = res.data.permissions && res.data.permissions.length > 0;
              this.$store.commit("SET_USER_TYPE", isAgent ? "agent" : "admin");

              setToken(res.data.token);
              localStorage.setItem(
                website.key + "-user",
                JSON.stringify(res.data.user)
              );
              this.$router.push({ path: "/wel/index" });
            })
            .catch(() => {
              this.loading = false;
            });
        } else {
          this.$message.error("请补全账号密码信息！");
          return false;
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.login-wrapper {
  h1 {
    font-size: 48px;
    color: #8dbafe;
    padding: 20px 0 0 100px;
  }

  .form-item {
    display: flex;
    color: #9b9b9b;
    font-size: 16px;

    .left {
      flex: 1;

      .el-checkbox__label {
        font-size: 16px;
      }

      .el-checkbox__inner {
        border-radius: 30px;
      }
    }

    .right {
      flex: 0 0 auto;

      .el-link {
        font-size: 16px;
      }
    }
  }

  .btn-login {
    display: block;
    width: 100%;
    height: 50px;
    line-height: 50px;
    padding: 0;
    margin: 40px 0 120px;
    font-size: 18px;

    font-family: PingFangSC-Medium, PingFang SC;
  }

  .notbn-login {
    display: block;
    width: 100%;
    height: 50px;
    line-height: 50px;
    padding: 0;
    margin: 40px 0 120px;
    font-size: 18px;

    font-family: PingFangSC-Medium, PingFang SC;
    background: #999 !important;
  }

  .input {
    display: inline-block;
    height: 47px;
    width: 85%;

    font-size: 18px;

    .el-input__inner {
      background-color: transparent;

      &:-internal-autofill-selected,
      &:-webkit-autofill:focus,
      &:-webkit-autofill {
        background-color: transparent !important;
        background-image: none !important;
        -webkit-appearance: menulist-button;
        box-shadow: inset 0 0 0 1000px rgba(32, 44, 60, 0.98) !important; // 改变了背景色
        -webkit-text-fill-color: #fff; // 改变了字体颜色
      }
    }

    input {
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      /* color: $light_gray; */
      height: 47px;
      /* caret-color: $cursor; */
      color: #000;

      &:-webkit-autofill {
        /* box-shadow: 0 0 0px 1000px $bg inset !important; */
        /* -webkit-text-fill-color: $cursor !important; */
      }
    }
  }

  .phoneinput {
    width: 62% !important;
  }

  .el-button.is-disabled:hover {
    background-color: transparent;
  }

  .qrbtn {
    width: 10%;
    border: none;
    color: #fff;
    background: transparent;
  }

  .el-form-item__content {
    width: 100%;
  }

  .el-form-item.form-input {
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 5px;
    color: #454545;

    border-radius: 5px;
    border: 1px solid #d8d8d8;
  }

  .form-radio {
    text-align: center;

    .radio {
      color: #aaa;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 2px;
    font-size: 16px;
    color: #c0c4cc;
    cursor: pointer;
    user-select: none;
  }
}
</style>

<style lang="scss" scoped>
.login-wrapper {
  .login-contains-wrapper {
    display: flex;
    // justify-content: center;
    margin: 0 auto;
    width: 70%;

    .login-img {
      // width: 724px;
    }

    .login-form {
      flex: 1;
      margin-left: 30px;
      position: relative;
      z-index: 99;

      h4 {
        text-align: center;
      }

      .profile-img-card {
        width: 100px;
        height: 100px;
        display: block;
        -moz-border-radius: 50%;
        -webkit-border-radius: 50%;
        border-radius: 50%;
        margin: -93px auto 30px;
        border: 5px solid #fff;
      }

      .profile-name-card {
        margin: 0 0 9px;
        text-align: center;
      }
    }
  }

  // }
}
</style>

<style rel="stylesheet/scss" lang="scss">
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  // background-image: url("../assets/images/login-background.jpg");
  background-size: cover;

  .login-head {
    background: #899fe1;
    border-radius: 3px 3px 0 0;
  }
}

.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.login-form-wrapper {
  .el-form-item {
    margin-bottom: 32px;
  }

  .el-input {
    height: 33px;

    input {
      border: none;
      border-bottom: 1px solid #d2d6de !important;
      height: 33px;
      color: #555 !important;
      border-radius: 0 !important;
    }
  }

  .input-icon {
    color: #333;
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  width: 33%;
  height: 33px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.login-code-img {
  height: 33px;
}
</style>
